package com.trg.flex.remote
{
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.Operation;
	import mx.rpc.remoting.RemoteObject;
	
	public class AsyncTokenCall extends RemoteCall
	{
		public function AsyncTokenCall()
		{
			super();
		}
		
		protected var responder:AsyncTokenCallResponder = new AsyncTokenCallResponder();
		
		protected function setToken(token:AsyncToken, callback:Function):void {
			responder.call = this;
			responder.callback = callback;
			token.addResponder(responder);
		}
		
		override public function applyOutcome():void {
			if (responder.event is FaultEvent) {
				applyFault(responder.event as FaultEvent);
			} else {
				applyResult(responder.event as ResultEvent);
			}
		}
	}
}
import mx.rpc.IResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import flash.events.Event;
import com.trg.flex.remote.FaultCodeHelper;
import com.trg.flex.remote.RemoteCall;
	
class AsyncTokenCallResponder implements IResponder {
	public var call:RemoteCall;
	public var callback:Function;
	public var event:Event;
	
	public function result(data:Object):void {
		event = data as ResultEvent;
		callback(call, "result");
	}
	
	public function fault(info:Object):void {
		event = info as FaultEvent;
		callback(call, FaultCodeHelper.faultCodeToOutcome(FaultEvent(info).fault.faultCode));
	}
}